package com.verisette.ncos.service.master.bizunit;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;

import org.corp.agi.common.exception.BusinessException;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bean.UserBean;
import org.corp.agi.service.bizunit.CommonBusinessUnit;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.dao.RoleManagementDAO;
import com.verisette.ncos.persistence.domain.MasterUserGroup;

public class EditUserGroupBU extends CommonBusinessUnit {

	private Logger logger = Logger.getLogger(EditUserGroupBU.class);

	private RoleManagementDAO roleManagementDAO;
	@Required
	@Autowired
	public void setRoleManagementDAO(RoleManagementDAO roleManagementDAO) {
		this.roleManagementDAO = roleManagementDAO;
	}	
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		if(logger.isInfoEnabled()) logger.info( this.getClass().getSimpleName() + ": execute" );
		
		MasterUserGroup userGroup = (MasterUserGroup)processContext.getValueObject();
		UserBean user = processContext.getUserBean();
		Date currentDate = new Date();
		
		// Check same userGroupName
		logger.debug("1.Check same userGroupName.");
		if(roleManagementDAO.findUserGroupName(userGroup)){
			// Same userName
			throw new BusinessException(ProjectConstant.MESSAGE_CODE_USG0001);
			
		}
		
		// Update UserGroup into MASTER_USER_GROUP
		logger.debug("2.Update UserGroup into MASTER_USER_GROUP.");
		roleManagementDAO.updateUserGroup(userGroup, user.getUsername(), currentDate);
		logger.debug("--> Update UserGroup Success !!! usgId : " + userGroup.getUsgId());
		
	}
	
}
